package com.soup.view;

import com.soup.dao.PersonDao;
import com.soup.domain.Person;
import com.soup.view.component.Context;
import com.soup.view.component.MassageTip;
import com.soup.view.value.LoginValue;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MoneyFrame extends Context implements ActionListener {


    private JTextField money = new JTextField(10);

    private PersonDao dao = new PersonDao();

    private JButton ok = new JButton("确认存钱");

    public MoneyFrame(int width, int height) {
        super(width, height);

        money.setBounds(60,0,width-120,40);
        this.add(money,"存钱",100);

        Map<String,Object> map = new HashMap<>();
        map.put("id", LoginValue.id);
        List<Person> query = dao.query(map);

        money.setText(query.get(0).getMoney()+"");

        ok.setBounds(60,100,width-120,40);
        this.add(ok,20);

        ok.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == ok){

            Map<String,Object> map = new HashMap<>();
            map.put("id", LoginValue.id);
            List<Person> query = dao.query(map);

            Person person = query.get(0);
            person.setMoney(Integer.parseInt(money.getText()));

            int update = dao.update(person);
            if (update > 0){
                MassageTip.showMassage(null,"存钱成功");
            }else{
                MassageTip.showMassage(null,"存钱失败");
            }
        }
    }
}
